/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.system;

import java.util.HashMap;
import java.util.Locale;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

/**
 *
 * @author Yusup Jauhari Shandi <in.visi>
 */
@ManagedBean
@SessionScoped
public class LocaleBean {
    private HashMap<String, Locale> locales = null;
    private Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
    
    public LocaleBean() {
            locales = new HashMap<>(4);            
            locales.put(
                "English",
                new Locale("en", "EN"));
            locales.put(
                "Indonesian",
                new Locale("id", "ID"));            
        }
    
    public void chooseLocaleFromLink(ActionEvent event) {
            //String current = event.getComponent().getId();
            String current = (String)event.getComponent().getAttributes().get("value");
            FacesContext context = FacesContext.getCurrentInstance();
            context.getViewRoot().setLocale((Locale)locales.get(current));
        }
    
    

    public Locale getLocale() {
        return locale;
    }

    public String getLanguage() {
        return locale.getLanguage();
    }

    public void setLanguage(String language) {
        locale = new Locale(language);
        FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
    }

}